﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Security;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using FredCK.FCKeditorV2;
using BLL;
using DTO;
using System.IO;
using BLL.Article;

    public partial class AddEditImage : BasePage
    {
        string _dirUrl = "";
        protected string dirUrl
        {
            get { return _dirUrl; }
            set { _dirUrl = value; }
        }
        string _dirPath = "";
        protected string dirPath
        {
            get { return _dirPath; }
            set { _dirPath = value; }
        }
        protected void Page_Init(object sender, EventArgs e)
        {
            int id = int.Parse(this.Request.QueryString["ID"]);
            this.dirUrl = Globals.ApplicationPath + "Upload/Thuvienhinhanh/" + id + "/";
            this.dirPath = Server.MapPath(dirUrl);
            if (!Directory.Exists(dirPath))
                Directory.CreateDirectory(dirPath);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadFile();
                string jscript = "function UploadComplete(){" + ClientScript.GetPostBackEventReference(LinkButton1, "") + "};";
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "FileCompleteUpload", jscript, true);
                flashUpload.QueryParameters = "ID=" + this.Request.QueryString["ID"];
                flashUpload.UploadPage = Globals.ApplicationPath + "Upload.cs";
                Server.UrlEncode(Request.QueryString.ToString());
            }
        }
        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            LoadFile();
        }
        private void LoadFile()
        {
            int id = int.Parse(this.Request.QueryString["ID"]);
            string url = Globals.ApplicationPath;
            if (!url.EndsWith("/"))
                url = url + "/";
            dirUrl = url + "Upload/Thuvienhinhanh/" + id + "/";
            DirectoryInfo dir = new DirectoryInfo(Server.MapPath(dirUrl));
            GridView1.DataSource = dir.GetFiles("*.*");
            GridView1.DataBind();
        }
        protected void btLoad_Click(object sender, EventArgs e)
        {
            LoadFile();
        }
        protected void btDelete_Click(object sender, EventArgs e)
        {
            int id = int.Parse(this.Request.QueryString["ID"]);
            string url = Globals.ApplicationPath;
            if (!url.EndsWith("/"))
                url = url + "/";
            dirUrl = url + "Upload/Thuvienhinhanh/" + id + "/";
            //
            foreach (GridViewRow row in GridView1.Rows)
            {
                string TenFile = ((Label)row.FindControl("lblTenfiles")).Text;
                bool isChecked = ((CheckBox)row.FindControl("chkSelect")).Checked;
                if (isChecked)
                {
                    DeleteFile(Server.MapPath(dirUrl) + TenFile + "");
                }
            }
            LoadFile();
        }
        /// <summary>
        /// Xóa một file được chọn
        /// </summary>
        /// <param name="fileName"></param>
        private void DeleteFile(string fileName)
        {
            try
            {
                FileInfo fi;
                if (System.IO.File.Exists(fileName) == true)
                {
                    fi = new FileInfo(fileName);
                    fi.Delete();
                }
            }
            catch (Exception ex) { ex.Message.ToString(); }
        }
        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            
        }
}